import java.nio.*;
import java.nio.channels.*;
import java.net.*;
import java.io.IOException;
  
try {
	SocketAddress address = new InetSocketAddress("localhost", 5555);
	SocketChannel client = SocketChannel.open(address);
	
	Thread.start {	//send message

		ByteBuffer buffer = ByteBuffer.allocate(64);
		ReadableByteChannel input = Channels.newChannel(System.in);
		while (true) {
			if (input.read(buffer) > 0) {
				buffer.flip()
				client.write(buffer)
				buffer.clear()
			}
		}
	}
	
	Thread.start {	//receive messages

		ByteBuffer buffer = ByteBuffer.allocate(64);
		WritableByteChannel out = Channels.newChannel(System.out);
		while (true) {
			if (client.read(buffer) > 0) {
				buffer.flip();
				out.write(buffer);
				buffer.clear();
			}
		}
	}
	
} catch (IOException ex) {
    ex.printStackTrace( );   
}